package cn.iocoder.yudao.module.picturebook.controller.admin.translation;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.picturebook.controller.admin.translation.vo.*;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.translation.TranslationDO;
import cn.iocoder.yudao.module.picturebook.service.translation.TranslationService;

@Tag(name = "管理后台 - 绘本中英翻译")
@RestController
@RequestMapping("/picturebook/translation")
@Validated
public class TranslationController {

    @Resource
    private TranslationService translationService;

    @PostMapping("/create")
    @Operation(summary = "创建绘本中英翻译")
    @PreAuthorize("@ss.hasPermission('picturebook:translation:create')")
    public CommonResult<Long> createTranslation(@Valid @RequestBody TranslationSaveReqVO createReqVO) {
        return success(translationService.createTranslation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新绘本中英翻译")
    @PreAuthorize("@ss.hasPermission('picturebook:translation:update')")
    public CommonResult<Boolean> updateTranslation(@Valid @RequestBody TranslationSaveReqVO updateReqVO) {
        translationService.updateTranslation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除绘本中英翻译")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('picturebook:translation:delete')")
    public CommonResult<Boolean> deleteTranslation(@RequestParam("id") Long id) {
        translationService.deleteTranslation(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除绘本中英翻译")
                @PreAuthorize("@ss.hasPermission('picturebook:translation:delete')")
    public CommonResult<Boolean> deleteTranslationList(@RequestParam("ids") List<Long> ids) {
        translationService.deleteTranslationListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得绘本中英翻译")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('picturebook:translation:query')")
    public CommonResult<TranslationRespVO> getTranslation(@RequestParam("id") Long id) {
        TranslationDO translation = translationService.getTranslation(id);
        return success(BeanUtils.toBean(translation, TranslationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得绘本中英翻译分页")
    @PreAuthorize("@ss.hasPermission('picturebook:translation:query')")
    public CommonResult<PageResult<TranslationRespVO>> getTranslationPage(@Valid TranslationPageReqVO pageReqVO) {
        PageResult<TranslationDO> pageResult = translationService.getTranslationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TranslationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出绘本中英翻译 Excel")
    @PreAuthorize("@ss.hasPermission('picturebook:translation:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTranslationExcel(@Valid TranslationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TranslationDO> list = translationService.getTranslationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "绘本中英翻译.xls", "数据", TranslationRespVO.class,
                        BeanUtils.toBean(list, TranslationRespVO.class));
    }

}